// pages/uploadcai/uploadcai.js
const util = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        z_cai1: '',
        z_cai2: '',
        z_cai3: '',
        z_cai4: '',
        w_cai1: '',
        w_cai2: '',
        w_cai3: ''
    },

    /**
     * 明日菜单提交
     */
    formSubmit: function (e) {
        // util.openErrorToast('失败');

        let openid = app.globalData.openid;
        let tempData = e.detail.value;
        let promise_data = {
            z_cai1: tempData.z_cai1,
            z_cai2: tempData.z_cai2,
            z_cai3: tempData.z_cai3,
            z_cai4: tempData.z_cai4,
            w_cai1: tempData.w_cai1,
            w_cai2: tempData.w_cai2,
            w_cai3: tempData.w_cai3,
            openid: openid
        }

        const url = app.globalData.httpBase + "/api/xcx/uploadCai";

        util.newhttp(url, 'POST', promise_data).then(res => {
            const tempData = res.data;
            if(tempData.code == 1){
                util.openToast('上传成功!');
                setTimeout(function(){
                    wx.switchTab({
                        url: '../caidan/caidan'
                    })
                }, 600)
            }else{
                util.openErrorToast('请重试!');
            }

        }).catch(err => {
            util.openErrorToast('网络错误');
        })




    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //查询明日菜单,如果有的话,获取到,并赋值
        const url = app.globalData.httpBase + "/api/xcx/getTomorrowCaidan";
        const that = this;
        util.newhttp(url).then(res => {
            const tempData = res.data;
            if (tempData.code == 1) {
                that.setData({
                    z_cai1: tempData.data.zcai1,
                    z_cai2: tempData.data.zcai2,
                    z_cai3: tempData.data.zcai3,
                    z_cai4: tempData.data.zcai4,
                    w_cai1: tempData.data.wcai1,
                    w_cai2: tempData.data.wcai2,
                    w_cai3: tempData.data.wcai3
                })
            }
        }).catch(err => {
            console.log(err);
        })
    },
})